package com.learn.jackson.annotation.jsonTypeInfo;

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.FieldDefaults;

/**
 * @author XuYang
 * @since 2025/6/10 11:49
 */
@Data
@NoArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
@AllArgsConstructor(staticName = "of")
public class JsonTypeInfoEntity {

    Admin admin;

    @Data
    @NoArgsConstructor
    @FieldDefaults(level = AccessLevel.PRIVATE)
    @JsonTypeInfo(
        use = JsonTypeInfo.Id.NAME,
        include = JsonTypeInfo.As.PROPERTY,
        // 类型字段名为 type
        property = "type"
    )
    @JsonSubTypes({
        @JsonSubTypes.Type(value = JsonTypeInfoEntity.Dog.class, name = "dog"),
        @JsonSubTypes.Type(value = JsonTypeInfoEntity.Cat.class, name = "cat")
    })
    public abstract static class Admin {
    }

    @Data
    @NoArgsConstructor
    @FieldDefaults(level = AccessLevel.PRIVATE)
    @AllArgsConstructor(staticName = "of")
    public static class Dog
        extends Admin
    {
        Long id;
    }

    @Data
    @NoArgsConstructor
    @FieldDefaults(level = AccessLevel.PRIVATE)
    @AllArgsConstructor(staticName = "of")
    public static class Cat
        extends Admin
    {
        String name;
    }
}
